<?php

namespace App\Http\BackendControllers;


use App\Jobs\SendMessage;
use App\Models\Notice;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

class NoticeController extends Controller
{
    public function index()
    {
        $data = Notice::paginate(15);
        return view('backend.notice.index',compact('data'));
    }

    public function store(Request $request){
        $data = $request->only('title','content');
        $rules = [
            'title' => 'required|min:4',
            'content' => 'required'
        ];
        $message = [
            'title.required' => "标题不能为空",
            'title.min' => '标题至少为四个字符',
            'content' => '内容不能为空'
        ];
        $validator = Validator::make($data,$rules,$message);
        if($validator->fails()){
            return err('',$validator->errors()->first());
        }


        $notice = Notice::create($data);
        dispatch(new SendMessage($notice));
        return res('','提交成功');
    }

}
